package img;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 10:20:57 2018/11/14
 * @version:2.0
 */
public class ImgFile {
    public String path;
    public String fileName;
    public BufferedImage bufferedImage;
    int[][] pixels;
    int[] pixelsFour;

    public ImgFile() {
    }

    public ImgFile(String path, String fileName) {
        this.path = path;
        this.fileName = fileName;
        this.bufferedImage = getImgObj(path, fileName);
        this.pixels = PixelsUtil.getImageRGB(this.bufferedImage);
        this.pixelsFour = getFourPoint();
    }

    //找出包含所有相同元素；
    public List<Pixel> containImg(ImgFile find, double same) {
        List<Pixel> sameArr = new ArrayList<Pixel>();
        int xLength = this.pixels.length - find.pixels.length;
        int yLength = this.pixels[0].length - find.pixels[0].length;
        for (int i = 0; i < xLength; i++) {
            for (int j = 0; j < yLength; j++) {
                //遍历大图所有像素点，找到相同的四个点的东西；判断中……517,175
                int fourSameNum = fourPixelSame(find, i, j);
                if (fourSameNum > 4 * same) {
                    int allSameNum = allPixelSame(find, i, j);
                    if (find.bufferedImage.getWidth() * find.bufferedImage.getHeight() * same > allSameNum) {
                        Pixel pixel = new Pixel();
                        pixel.x = i;
                        pixel.y = j;
                        sameArr.add(pixel);
                    }
                }
            }
        }
        return sameArr;
    }

    //是否包含图片
    public int allPixelSame(ImgFile find, int w, int h) {
        int sameNum = 0;
        for (int i = 0; i < find.pixels.length; i++) {
            for (int j = 0; j < find.pixels[i].length; j++) {
                boolean same = this.pixels[i + w][j + h] == find.pixels[i][j];
                if (same) {
                    sameNum++;
                }
            }
        }
        return sameNum;
    }

    //是否包含图片4个像素点是否相同
    public int fourPixelSame(ImgFile find, int x, int y) {
        int sameNum = 0;
        int[] pixelsFourThis = getFourPointByFind(x, y, find);
        int[] pixelsFourfind = find.pixelsFour;
        for (int i = 0; i < 4; i++) {
            boolean same = pixelsFourThis[i] == pixelsFourfind[i];
            if (same) {
                sameNum++;
            }
        }
        return sameNum;
    }

    //获取4个顶点
    public int[] getFourPointByFind(int x, int y, ImgFile find) {
        int[] pixelsFour = new int[4];
        int w = find.pixels.length;
        int h = find.pixels[0].length;
        pixelsFour[0] = this.pixels[x][y];
        pixelsFour[1] = this.pixels[x + w - 1][y];
        pixelsFour[2] = this.pixels[x][y + h - 1];
        pixelsFour[3] = this.pixels[x + w - 1][y + h - 1];
        return pixelsFour;
    }

    //获取4个顶点
    public int[] getFourPoint() {
        int[] pixelsFour = new int[4];
        int w = this.pixels.length;
        int h = this.pixels[0].length;
        pixelsFour[0] = this.pixels[0][0];
        pixelsFour[1] = this.pixels[w - 1][0];
        pixelsFour[2] = this.pixels[0][h - 1];
        pixelsFour[3] = this.pixels[w - 1][h - 1];
        return pixelsFour;
    }

    //文件获取图像对象
    public BufferedImage getImgObj(String path, String fileName) {
        File imageFile = new File(path, fileName);
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            return bufferedImage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 统计图片颜色出现次数；
     */
    public void getRGBNum() {
        Map<Integer, Integer> rgbNum = new HashMap<Integer, Integer>();
        int[][] result = this.pixels;
        int width = result.length;
        int height = result[0].length;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                Integer index = new Integer(result[i][j]);
                Integer times = rgbNum.get(index);
                if (times == null) {
                    rgbNum.put(index, 1);
                } else {
                    rgbNum.put(index, times + 1);
                }
            }
        }
        System.out.println(rgbNum);
    }
}
